package com.sxz.sbs.module.order;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.sxz.sbs.R;
import com.sxz.sbs.app.GlideApp;
import com.sxz.sbs.applibrary.component.LabelTextView;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.order.mode.IntentOrder;
import com.sxz.sbs.module.order.mode.OrderDetailBean;
import com.sxz.sbs.module.order.presenter.OrderDetailPresenter;
import com.sxz.sbs.module.order.view.IOrderDetailView;
import com.sxz.sbs.module.photo.PhotoViewerActivity;
import com.sxz.sbs.module.photo.PhotoViewerBean;
import com.sxz.sbs.other.FileBean;
import com.sxz.sbs.other.ImageOrderAdapter;
import com.sxz.sbs.view.LabelLinearLayout;
import com.sxz.sbs.view.SelfAdaptionGridView;
import com.sxz.sbs.widget.CommonTitle;
import com.sxz.sbs.widget.PhoneDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 订单详情
 *
 * @author tian
 * @date 2018/4/10
 */
public class OrderDetailActivity extends BaseActivity<OrderDetailPresenter> implements IOrderDetailView {

    @BindView(R.id.iv_order)
    ImageView ivOrder;
    @BindView(R.id.tv_order_title)
    TextView tvOrderTitle;
    @BindView(R.id.tv_order_parkingSpace_number)
    TextView tvOrderParkingSpaceNumber;
    @BindView(R.id.lll_label)
    LabelLinearLayout lllLabel;
    @BindView(R.id.tv_order_price)
    TextView tvOrderPrice;
    @BindView(R.id.ll_order)
    LinearLayout llOrder;
    @BindView(R.id.iv_order_status)
    ImageView ivOrderStatus;
    @BindView(R.id.tv_order_detail_no)
    TextView tvOrderDetailNo;
    @BindView(R.id.tv_order_detail_type)
    TextView tvOrderDetailType;
    @BindView(R.id.tv_order_detail_status)
    TextView tvOrderDetailStatus;
    @BindView(R.id.tv_order_detail_parking)
    TextView tvOrderDetailParking;
    @BindView(R.id.tv_order_detail_address)
    TextView tvOrderDetailAddress;
    @BindView(R.id.tv_order_detail_space_number)
    TextView tvOrderDetailSpaceNumber;
    @BindView(R.id.tv_order_detail_price)
    TextView tvOrderDetailPrice;
    @BindView(R.id.tv_order_detail_discounts)
    TextView tvOrderDetailDiscounts;
    @BindView(R.id.tv_order_detail_capital)
    TextView tvOrderDetailCapital;
    @BindView(R.id.tv_order_detail_total_price)
    TextView tvOrderDetailTotalPrice;
    @BindView(R.id.tv_order_detail_stages)
    TextView tvOrderDetailStages;
    @BindView(R.id.tv_order_detail_order_date)
    TextView tvOrderDetailOrderDate;
    @BindView(R.id.tv_order_detail_pay_date)
    TextView tvOrderDetailPayDate;
    @BindView(R.id.tv_order_detail_complete_date)
    TextView tvOrderDetailCompleteDate;
    @BindView(R.id.tv_order_detail_salesman)
    TextView tvOrderDetailSalesman;
    @BindView(R.id.tv_order_detail_user)
    TextView tvOrderDetailUser;
    @BindView(R.id.tv_order_detail_phone)
    TextView tvOrderDetailPhone;
    @BindView(R.id.tv_order_detail_remakes)
    TextView tvOrderDetailRemakes;
    @BindView(R.id.tv_order_detail_bottom)
    TextView tvOrderDetailBottom;
    @BindView(R.id.ll_order_detail_bottom)
    LinearLayout llOrderDetailBottom;
    @BindView(R.id.iv_title_back)
    ImageView ivTitleBack;
    @BindView(R.id.iv_title_left_img)
    ImageView ivTitleLeftImg;
    @BindView(R.id.tv_nav_title)
    TextView tvNavTitle;
    @BindView(R.id.iv_title_standby)
    ImageView ivTitleStandby;
    @BindView(R.id.tv_title_standby)
    TextView tvTitleStandby;
    @BindView(R.id.iv_title_more)
    ImageView ivTitleMore;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.rely_title)
    RelativeLayout relyTitle;
    @BindView(R.id.ltv_allot_order)
    LabelTextView ltvAllotOrder;
    @BindView(R.id.ltv_modify_order)
    LabelTextView ltvModifyOrder;
    @BindView(R.id.rl_order_detail_close)
    RelativeLayout rlCloseOrder;
    @BindView(R.id.tv_order_detail_close)
    TextView tvCloseReason;
    @BindView(R.id.tv_photo_type_explain)
    TextView tvPhotoTypeExplain;
    @BindView(R.id.order_detail_grid_view)
    SelfAdaptionGridView orderDetailGridView;
    @BindView(R.id.ll_pic)
    LinearLayout llPic;
    private String phone;
    private OrderDetailBean detailData;
    private int orderId;
    private List<FileBean> imgList;
    private ImageOrderAdapter adapter;
    private  List<String> urlList = new ArrayList<>();
    @Override
    protected void init() {
        new CommonTitle.Builder(this).setTitle("订单详情").build().show();
        orderId = getIntent().getIntExtra("order_id", 0);
        initGridView();
    }
    /**
     * 初始化图片列表
     */
    private void initGridView() {
        if (imgList == null) {
            imgList = new ArrayList<>();
        }
        adapter = new ImageOrderAdapter(this,imgList,3);
        orderDetailGridView.setAdapter(adapter);
        adapter.setOnImageShowListener(new ImageOrderAdapter.OnImageShowListener() {
            @Override
            public void onImageClick(View v, int position) {
                urlList.clear();
                for (int i = 0; i < imgList.size(); i++) {
                    if (imgList.get(i) != null) {
                        urlList.add(imgList.get(i).getPath());
                    }
                }
                PhotoViewerBean pvb = new PhotoViewerBean(urlList,position) ;
                startActivity(new Intent(OrderDetailActivity.this, PhotoViewerActivity.class)
                        .putExtra(PhotoViewerActivity.PHOTO_VIEWER_TAG, pvb));
            }

        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        HashMap<String, Object> map = HttpParamMap.getParamMap();
        map.put("orderId", orderId);
        mPresenter.getOrderDetail(map);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_order_detail;
    }

    @Override
    protected OrderDetailPresenter createPresenter() {
        return new OrderDetailPresenter();
    }

    @Override
    public void onGetOrderDetailSuccess(OrderDetailBean data) {
        if (mIsLive && data != null) {
            detailData = data;
            tvOrderTitle.setText(data.getTitle());
            GlideApp.with(this).load(data.getPhotoUrl())
                    .placeholder(R.drawable.img_default_stall)
                    .error(R.drawable.img_default_stall).into(ivOrder);
            tvOrderParkingSpaceNumber.setText("车位编号：" + data.getNumber());
            lllLabel.clearChild();
            lllLabel.setOrderDetailLabels(data.getDiscount());
            tvOrderPrice.setText("¥" + data.getAdvanceMoney());
            if (data.getStatus() == 6) {
                ivOrderStatus.setVisibility(View.VISIBLE);
                if (data.getAuditStatus() == 1) {
                    ivOrderStatus.setImageResource(R.drawable.icon_order_complete);
                } else {
                    ivOrderStatus.setImageResource(R.drawable.icon_order_wait);
                }
            } else if (data.getStatus() == 1) {
                ivOrderStatus.setVisibility(View.VISIBLE);
                if (data.getAuditStatus() == 1) {
                    ivOrderStatus.setImageResource(R.drawable.icon_order_auth);
                } else {
                    ivOrderStatus.setImageResource(R.drawable.icon_order_wait);
                }
            } else {
                ivOrderStatus.setVisibility(View.GONE);
            }
            tvOrderDetailNo.setText(data.getOrderNumber());
            tvOrderDetailType.setText(data.getOrderType());
            tvOrderDetailStatus.setText(data.getStatusStr());
            tvOrderDetailParking.setText(data.getName());
            tvOrderDetailAddress.setText(data.getAddress());
            tvOrderDetailSpaceNumber.setText(data.getNumber());
            tvOrderDetailPrice.setText("¥" + data.getAdvanceMoney());

            // 设置折扣
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < data.getDiscount().size(); i++) {
                stringBuilder.append(data.getDiscount().get(i).getTitle() + ",");
            }
            if (!TextUtils.isEmpty(stringBuilder.toString())) {
                tvOrderDetailDiscounts.setText(stringBuilder.toString().substring(0, stringBuilder.toString().length() - 1));
            }

            tvOrderDetailCapital.setText(data.getAdvanceMoneyStr());
            tvOrderDetailTotalPrice.setText("¥" + data.getDealPrice());
            tvOrderDetailStages.setText(data.getIsByStage());
            tvOrderDetailOrderDate.setText(data.getCreateTime());
            tvOrderDetailPayDate.setText(data.getHoldBeginTime());
            tvOrderDetailCompleteDate.setText(data.getHoldEndTime());
            tvOrderDetailSalesman.setText(data.getTxt_Name() + "  " + data.getTxt_Account());
            tvOrderDetailUser.setText(data.getPayUserName());
            tvOrderDetailPhone.setText(data.getPayUserPhone());
            phone = data.getPayUserPhone();
            tvOrderDetailRemakes.setText(data.getRemark());
            //if (data.getDealStatus() == 0) {
                /*llOrderDetailBottom.setVisibility(View.VISIBLE);
                tvOrderDetailBottom.setVisibility(View.GONE);*/
            if (data.getStatus() != 4 && data.getStatus() != 5 &&
                    data.getStatus() != 7
                    ) {
                if (data.getStatus() == 1) {
                    if (data.getAuditStatus() == 1) {
                        ltvModifyOrder.setVisibility(View.VISIBLE);
                    } else {
                        ltvModifyOrder.setVisibility(View.GONE);
                    }
                } else if (data.getStatus() == 6) {
                    ltvModifyOrder.setVisibility(View.GONE);
                } else {
                    ltvModifyOrder.setVisibility(View.VISIBLE);
                }
            } else {
                ltvModifyOrder.setVisibility(View.GONE);
            }
            // }
            if (data.getIsLeader() == 1) {
                //是案场负责人
                if (data.getDealStatus() == 1) {
                    //  已分配
                    tvOrderDetailBottom.setVisibility(View.VISIBLE);
                    llOrderDetailBottom.setVisibility(View.GONE);
                    tvOrderDetailBottom.setText("已分配至 " + data.getTxt_Name() + " 处理");
                } else {
                    tvOrderDetailBottom.setVisibility(View.GONE);
                    llOrderDetailBottom.setVisibility(View.VISIBLE);
                    ltvAllotOrder.setVisibility(View.VISIBLE);
                }
            } else {
                tvOrderDetailBottom.setVisibility(View.GONE);
                llOrderDetailBottom.setVisibility(View.VISIBLE);
                ltvAllotOrder.setVisibility(View.GONE);
            }
            if (data.getStatus() == 5 || data.getStatus() == 7) {
                rlCloseOrder.setVisibility(View.VISIBLE);
                tvCloseReason.setText(data.getFailReason());
            } else {
                rlCloseOrder.setVisibility(View.GONE);
            }

            if (data.getStatus() == 6) {
                if (data.getProtocolList().size() <= 0) {
                    return;
                }
                notifys(data);
                llPic.setVisibility(View.VISIBLE);
                tvPhotoTypeExplain.setText("合同照片");
            } else if (data.getStatus() == 1) {
                if (data.getProtocolList().size() <= 0) {
                    return;
                }
                notifys(data);
                llPic.setVisibility(View.VISIBLE);
                tvPhotoTypeExplain.setText("定金凭证");
            } else if (data.getStatus() == 2) {
                if (data.getProtocolList().size() <= 0) {
                    return;
                }
                notifys(data);
                llPic.setVisibility(View.VISIBLE);
                tvPhotoTypeExplain.setText("退款流水图");
            } else {
                llPic.setVisibility(View.GONE);
            }







        }

    }
        private void notifys(OrderDetailBean data) {
            imgList.clear();
            for (int i = 0; i < data.getProtocolList().size(); i++) {
                imgList.add(new FileBean(data.getProtocolList().get(i).getId(),data.getProtocolList().get(i).getPhotoUrl()));
            }
            adapter.setNotify(imgList);
        }
    @OnClick({R.id.ltv_allot_order, R.id.ltv_call_order, R.id.ltv_modify_order})
    public void onViewClicked(View view) {
        if (detailData == null) {
            return;
        }
        switch (view.getId()) {
            case R.id.ltv_allot_order:
                IntentOrder intentOrder = new IntentOrder();
                intentOrder.setOrderId(detailData.getId());
                intentOrder.setParkingId(detailData.getParkingId());
                startActivity(new Intent(this, AllotOrderActivity.class)
                        .putExtra("intent_order", intentOrder));
                break;
            case R.id.ltv_call_order:
                PhoneDialog.callPhone(this, phone);
                break;
            case R.id.ltv_modify_order:
                IntentOrder intentOrder1 = new IntentOrder();
                intentOrder1.setOrderId(detailData.getId());
                intentOrder1.setIsByStage(detailData.getIsByStage());
                intentOrder1.setPrice(detailData.getDealPrice() + "");
                intentOrder1.setStatusInt(detailData.getStatus());
                intentOrder1.setRemark(detailData.getRemark());
                startActivity(new Intent(this, OrderModifyActivity.class)
                        .putExtra("intent_order", intentOrder1));
                break;
            default:
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
